﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class KhachHang_DAO
    {
        private static KhachHang_DAO m_instance = null;

        public static KhachHang_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new KhachHang_DAO();
                return m_instance;
            }
        }

        /// <summary>
        /// Lấy thông tin khách hàng bằng Mã Khách Hàng
        /// </summary>
        /// <param name="id">Mã khách hàng</param>
        /// <returns></returns>
        public DataTable LayKhachHang(string id)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYKHACHHANGBYID", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaKhachHang", SqlDbType.NVarChar, 10);
                cmd.Parameters["@MaKhachHang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Lấy toàn bộ khách hàng
        /// </summary>
        /// <returns></returns>
        public DataTable LayToanBoKhachHang()
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_TOANBO_KHACHHANG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@makhachhang", SqlDbType.Char, 10);
                //cmd.Parameters["@makhachhang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Lấy toàn bộ mã khách hàng
        /// </summary>
        /// <returns></returns>
        public DataTable LayToanBoMaKhachHang()
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_TOANBO_MAKHACHHANG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@makhachhang", SqlDbType.Char, 10);
                //cmd.Parameters["@makhachhang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Lấy toàn bộ mã khách hàng quen
        /// </summary>
        /// <returns></returns>
        public DataTable LayToanBoMaKhachHangQuen()
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_TOANBO_MAKHACHHANG_QUEN", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@makhachhang", SqlDbType.Char, 10);
                //cmd.Parameters["@makhachhang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        ///  Cập nhật thông tin Khách hàng
        /// </summary>
        /// <param name="makh"></param>
        /// <param name="tenkh"></param>
        /// <param name="diachi"></param>
        /// <returns></returns>
        public bool CapNhatKhachHang(string makh, string tenkh, string diachi, bool thanthiet)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("UPDATE_KHACHHANG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaKhachHang", SqlDbType.Char, 10);
                cmd.Parameters["@MaKhachHang"].Value = makh;
                cmd.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar, 40);
                cmd.Parameters["@TenKhachHang"].Value = tenkh;
                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 50);
                cmd.Parameters["@DiaChi"].Value = diachi;
                cmd.Parameters.Add("@LoaiKhachHang", SqlDbType.Int);
                cmd.Parameters["@LoaiKhachHang"].Value = (thanthiet ? 1 : 0);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        ///   tim kiem khach hang
        /// </summary>
        /// 
        public DataTable TimKiemKhachHang(string _id, string _name, string _address, bool _thanthiet)
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("FIND_KHACHHANG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaKhachHang", SqlDbType.NVarChar, 40);
                cmd.Parameters["@MaKhachHang"].Value = _id;

                cmd.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar, 100);
                cmd.Parameters["@TenKhachHang"].Value = _name;

                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 100);
                cmd.Parameters["@DiaChi"].Value = _address;

                cmd.Parameters.Add("@LoaiKhachHang", SqlDbType.Int);
                cmd.Parameters["@LoaiKhachHang"].Value = (_thanthiet ? 1 : 0);

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;

            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// Xóa Khách Hàng
        /// </summary>
        /// <param name="makh"></param>
        /// <returns></returns>
        public bool XoaKhachHang(string makh)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("REMOVE_KHACHHANG", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaKhachHang", SqlDbType.Char, 10);
                cmd.Parameters["@MaKhachHang"].Value = makh;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Thêm một Khách hàng mới
        /// </summary>
        /// <param name="tenkh"></param>
        /// <param name="diachi"></param>
        /// <param name="thanthiet"></param>
        /// <returns></returns>
        public bool ThemKhachHangMoi(string tenkh, string diachi, bool thanthiet)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_KHACHHANG_MOI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar, 40);
                cmd.Parameters["@TenKhachHang"].Value = tenkh;
                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 50);
                cmd.Parameters["@DiaChi"].Value = diachi;
                cmd.Parameters.Add("@LoaiKhachHang", SqlDbType.Int);
                cmd.Parameters["@LoaiKhachHang"].Value = thanthiet ? 1 : 0;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
